home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / mkmf / mkmf.md < prev    next >
Encoding:
Text File  |  1992-06-17  |  5.5 KB  |  215 lines

  1. #!/sprite/cmds/csh -f
  2. #
  3. # A script to generate the Makefile for a subdirectory that contains
  4. # machine-dependent sources and objects.  If ./Makefile.proto exists,
  5. # use it, else use a default prototype.
  6. #
  7. # We assume we were invoked from mkmf.  Parameters passed in from mkmf
  8. # through environment variables:
  9. #
  10. # Parameters passed in from mkmf as environment variables:
  11. #    MKMFDIR        directory containing prototype makefiles
  12. #    MAKEFILE    name of makefile to create
  13. #    PARENTDIR    name of parent directory (.. is fooled by symbolic
  14. #            links)
  15. #            
  16. # Variables generated here:
  17. #    dir        name of this subdirectory
  18. #    machine        name of machine type for this directory (e.g. "sun3")
  19. #    pref        prefix pattern that files must match to be included
  20. #    makefile    name of the makefile to create
  21. #    proto        name of prototype makefile to use to create $makefile
  22. #
  23.  
  24. #
  25. # Argument processing.  (Generalized form, even though just one flag so far.)
  26. #
  27. while ($#argv >= 1)
  28.     if ("$1" == '-x') then
  29.     set echo
  30.     endif
  31.     shift
  32. end
  33.  
  34. set machine=$cwd:t
  35. set machine=$machine:r
  36. set pref='[0-9a-z_A-Z]'
  37.  
  38. if ($?MAKEFILE) then
  39.     set makefile=$MAKEFILE
  40. else
  41.     set makefile=Makefile
  42. endif
  43.  
  44. if (-e $makefile.proto) then
  45.     set proto=$makefile.proto
  46. else
  47.     set proto="${MKMFDIR}/Makefile.md"
  48. endif
  49.  
  50. set dir=$cwd:t
  51. set machine=$dir:r
  52. set parent=$cwd:h
  53. set parent=$parent:t
  54. echo "Generating $makefile for $parent/$dir using $proto"
  55.  
  56. if ($?PARENTDIR) then
  57.     set parentdir=$PARENTDIR
  58. else
  59.     set parentdir=..
  60. endif
  61.  
  62. #
  63. # First figure out what's here by way of .c, .y, .l, .s, .p, .h
  64. # and .o files.  For sources, look both in this directory and in
  65. # the parent (machine-independent) directory.
  66. # If any one doesn't have any members, it'll contain the original
  67. # pattern (b/c of nonomatch). We want it to be empty, though, so
  68. # we reset it.
  69. #
  70. set nonomatch
  71. set tmp=( ${pref}*.{c,y,l,s,p,cc} )
  72. #
  73. # Check to see if there were any sources.  The first check (size == 1)
  74. # is only necessary because the second check will cause an error if
  75. # allSrcs contains more than 1024 bytes.
  76. #
  77. if ($#tmp == 1) then
  78.     if ("$tmp" == "${pref}*.{c,y,l,s,p,cc}") set tmp=()
  79. endif
  80. set srcs=()
  81. foreach i ($tmp)
  82.     set srcs=($srcs $dir/$i)
  83. end
  84. set tmp=( ${parentdir}/${pref}*.{c,y,l,s,p,cc} )
  85. if ($#tmp == 1) then
  86.     if ("$tmp" == "${parentdir}/${pref}*.{c,y,l,s,p,cc}") set tmp=()
  87. endif
  88.  
  89. foreach i ($tmp:gt)
  90.     if (! -e $i) then
  91.     set srcs = ($srcs $i)
  92.     endif
  93. end
  94.  
  95. set tmp=( ${pref}*.h )
  96. if ($#tmp == 1) then
  97.     if ("$tmp" == "${pref}*.h") set tmp=()
  98. endif
  99. set hdrs=()
  100. foreach i ($tmp)
  101.     set hdrs=($hdrs $dir/$i)
  102. end
  103. set tmp=( ${parentdir}/${pref}*.h )
  104. if ($#tmp == 1) then
  105.     if ("$tmp" == "${parentdir}/${pref}*.h") set tmp=()
  106. endif
  107. foreach i ($tmp:gt)
  108.     if (! -e $i) then
  109.     set hdrs = ($hdrs $i)
  110.     endif
  111. end
  112.  
  113. set tmp=( ${parent}*.h )
  114. if ($#tmp == 1) then
  115.     if ("$tmp" == "${parent}*.h") set tmp=()
  116. endif
  117. set pubHdrs=()
  118. foreach i ($tmp)
  119.     set pubHdrs=($pubHdrs $dir/$i)
  120. end
  121. set pubHdrs=(`echo $pubHdrs | sed -e "s/[^ ]*Int.h//g"`)
  122.  
  123. #
  124. # Find miscellaneous files needed for builds (Makefiles, etc).
  125. #
  126. set tmp=( md.mk md.mk.sed dependencies.mk )
  127. set instfiles=()
  128. foreach i ($tmp)
  129.     if (-e $i) then
  130.     set instfiles=($instfiles $dir/$i)
  131.     endif
  132. end
  133. set tmp=( Makefile local.mk Makefile.sed Makefile.ex tags TAGS )
  134. foreach i ($tmp)
  135.     if (-e ${parentdir}/$i) then
  136.     set instfiles = ($instfiles $i)
  137.     endif
  138. end
  139.  
  140.  
  141. rm -f version.o
  142. set tmp=( ${pref}*.o )
  143. #
  144. # Check to see if there were any object files.  The first check (size == 1)
  145. # is only necessary because the second check will cause an error if
  146. # tmp contains more than 1024 bytes.
  147. #
  148. if ($#tmp == 1) then
  149.     if ("$tmp" == "${pref}*.o") set tmp=()
  150. endif
  151. set objs=()
  152. foreach i ($tmp)
  153.     set objs=($objs $dir/$i)
  154. end
  155. unset nonomatch
  156.  
  157. #
  158. # Merge in any .o files that can be created from local source files but don't
  159. # exist yet. In addition, figure out which .o files may be safely removed
  160. # during a "make clean" and store them in RmOfiles.
  161. # Also figure out if there are any generated .c files (like from lex and
  162. # yacc) that should be removed.
  163. #
  164. set RmOfiles=""
  165. set RmCfiles=""
  166. if ($#srcs != 0) then
  167.     foreach file ($srcs)
  168.         set file=$file:t
  169.         set file=$file:r.o
  170.         set RmOfiles=($RmOfiles $dir/$file)
  171.         if (! -e $file) set objs=($objs $dir/$file)
  172.     end
  173.     foreach file ($srcs)
  174.         if (("$file:e" == "l") || ("$file:e" == "y")) then
  175.             set RmCfiles=($RmCfiles $file:r.c)
  176.         endif
  177.     end
  178. endif
  179.  
  180. set sacredObjs=""
  181. foreach file ($objs)
  182.     set tmp = $file:r
  183.     echo $RmOfiles | grep $file > /dev/null
  184.     if ($status && ($tmp:t != $parentdir:t)) then
  185.     set sacredObjs = ($sacredObjs $file)
  186.     echo "WARNING: file $file does not have a source file"
  187.     endif
  188. end
  189.  
  190. #
  191. # Use sed to substitute various interesting things into the prototype
  192. # makefile. The code below is a bit tricky because some of the variables
  193. # being substituted in can be very long:  if the substitution is passed
  194. # to sed with "-e", the entire variable must fit in a single shell argument,
  195. # with a limit of 1024 characters.  By generating a separate script file
  196. # for the very long variables, the variables get passed through (to the
  197. # script file) as many arguments, which gets around the length problem.
  198. #
  199.  
  200. set rmfiles = ($RmOfiles $RmCfiles)
  201. rm -f mkmf.tmp.sed
  202. echo s,"@(SRCS)",$srcs,g > mkmf.tmp.sed
  203. echo s,"@(OBJS)",$objs,g >> mkmf.tmp.sed
  204. echo s,"@(CLEANOBJS)",$rmfiles,g >> mkmf.tmp.sed
  205. echo s,"@(HDRS)",$hdrs,g >> mkmf.tmp.sed
  206. echo s,"@(INSTFILES)",$instfiles,g >> mkmf.tmp.sed
  207. echo s,"@(SACREDOBJS)",$sacredObjs,g >> mkmf.tmp.sed
  208.  
  209. cat $proto | sed -f mkmf.tmp.sed \
  210.     -e "s,@(PUBHDRS),$pubHdrs,g" \
  211.     -e "s,@(TEMPLATE),$proto,g" \
  212.     -e "s,@(DATE),`date`,g" > $makefile
  213. rm -f mkmf.tmp.sed
  214.